<?php

defined('SYSPATH') or die('No direct script access.');

class Ad {

    private static $instances;
    private $data;

    public static function & instances($id = 0) {

        if (!isset(self::$instances[$id])) {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id) {

        $this->data = NULL;
        $this->_load($id);
    }

    public function _load($id) {

        if (!$id)
            return FALSE;

        $result = ORM::factory('ad')
                ->where('id_ad', '=', $id)
                ->find()
                ->as_array();

        $this->data = $result ? $result : array();
    }

    public function get($key = NULL) {

        if (is_null($key)) {
            return $this->data;
        } else {
            return isset($this->data[$key]) ? $this->data[$key] : NULL;
        }
    }

    public function set($data) {

        $ad = ORM::factory('ad');
        $ad->values($data);
        if ($ad->check()) {
            $ad->save();
            if ($ad->saved()) {
                $this->add_ads_images($ad->id_ad);
                return true;
            }
        }

        return false;
    }

    public function add_ads_images($id_ad) {
        $imagesfile_define = Session::instance()->get('imagesfile_define');
        if (is_array($imagesfile_define)) {
            foreach ($imagesfile_define as $key => $value) {
                $ads_image = ORM::factory('ads_image');
                $ads_image->id_adsimage = $id_ad;
                $ads_image->url_image = $value;
                $ads_image->addtime = time();
                if ($ads_image->check()) {
                    $ads_image->save();
                }
                if (Session::instance()->get('temp_imagefiles')) {
                    Session::instance()->delete('temp_imagefiles');
                }

                if (Session::instance()->get('imagesfile_define')) {
                    Session::instance()->delete('imagesfile_define');
                }
            }
        }
    }

    public function update($data) {
        $ad = ORM::factory('ad')
                ->where('id_ad', '=', $this->data['id_ad'])
                ->find();

        if (!$ad->loaded()) {
            return false;
        } else {
            $ad->values($data);
            if ($ad->check()) {
                $ad->save();
                if ($ad->saved()) {
                    $this->add_ads_images($ad->id_ad);
                    return true;
                }
            }
        }
    }

    public function delete() {
        $ad = ORM::factory('ad', $this->data['id_ad']);
        if ($ad->loaded()) {
            return $ad->delete();
        } else {
            return FALSE;
        }
    }

    public function get_ads($id_category) {

        $ads = ORM::factory('ad')
                ->where('id_category', '=', $id_category)
                ->order_by('id_ad')
                ->find_all()
                ->as_array();

        $images_temp = array();
        foreach ($ads as $key => $value) {

            $images_temp[$value->id_ad]['id_ad'] = $value->id_ad;
            $images_temp[$value->id_ad]['name'] = $value->name;
            $images_temp[$value->id_ad]['description'] = $value->description;
            $images_temp[$value->id_ad]['images'] = ORM::factory('ads_image')
                            ->where('id_adsimage', '=', $value->id_ad)
                            ->order_by('id_image')
                            ->limit('1')
                            ->find()->as_array();
        }
        $ads = $images_temp;
        unset($images_temp);

        return $ads;
    }

}

